package com.scilvet.reportcomposer.ui;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;

import com.scilvet.reportcomposer.ReportComposer;

public class SwingUserInterface extends UserInterface implements ActionListener {
	
	private static final String SELECT_ACCOUNT = "selectAccount";
	private static final String COMPUTE = "compute";
	private static final String REPORT_SELECTED = "reportSelected";
	
	private JFrame frame = null;
	private JFileChooser fileChooser = null;
	private JLabel fileLabel = null;
	private String reportFilename = null;
	private File accountFile = null;
	
	private static final String EXTENSION = ".csv";

	public SwingUserInterface(ReportComposer reportComposer) {
		super(reportComposer);
	}
	
	private void setupGUI() {
		GridBagConstraints gbc;
		JLabel label;
		JButton button;
		Container container;
		JComboBox comboBox;
		
		frame = new JFrame("Report composer");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		container = frame.getContentPane();
		fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new FileFilter() {
			
			@Override
			public boolean accept(File file) {
				
				if (file.isDirectory()) {
					return true;
				}
				
				String filename = file.getName();
				return filename.endsWith(EXTENSION);
			}

			@Override
			public String getDescription() {
				return "Comma Separate Value .csv";
			}
		});
		
		container.setLayout(new GridBagLayout());
		gbc = new GridBagConstraints();
		gbc.insets = new Insets(5, 5, 5, 5);
		
		// Setting up report selection
		gbc.gridx = 0;
		gbc.gridy = 0;
		label = new JLabel("Report");
		container.add(label, gbc);
		
		String[] availableReports = reportComposer.getAvailableReports();
		reportFilename = availableReports[0];
		gbc.gridx = 1;
		gbc.gridy = 0;
		comboBox = new JComboBox(availableReports);
		comboBox.setActionCommand(REPORT_SELECTED);
		comboBox.addActionListener(this);
		container.add(comboBox, gbc);
		
		// Setting up file chooser button
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridwidth = 2;
		button = new JButton("Seleziona file conti");
		button.setActionCommand(SELECT_ACCOUNT);
		button.addActionListener(this);
		container.add(button, gbc);
		
		gbc.gridy = 2;
		fileLabel = new JLabel("No file selected...");
		container.add(fileLabel, gbc);
		
		// Setting up compute button
		gbc.gridx = 0;
		gbc.gridy = 3;
		button = new JButton("Crea report");
		button.setActionCommand(COMPUTE);
		button.addActionListener(this);
		container.add(button, gbc);
		
		frame.pack();
		frame.setVisible(true);
	}

	@Override
	public void showMain() {
		
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				setupGUI();
			}
		});
	}

	@Override
	public void showNotification(String message) {
		JOptionPane.showMessageDialog(frame.getContentPane(), message);
	}

	@Override
	public void showErrorNotification(String errorMessage) {
		JOptionPane.showMessageDialog(frame.getContentPane(), errorMessage);
		System.exit(-1);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();
		
		if (actionCommand.equals(SELECT_ACCOUNT)) {
			
			int success = fileChooser.showOpenDialog(frame);
			
			if (success != JFileChooser.APPROVE_OPTION) {
				return;
			}
			
			accountFile = fileChooser.getSelectedFile();
			fileLabel.setText("File: " + accountFile.getName());
			
		} else if (actionCommand.equals(REPORT_SELECTED)){
			
			reportFilename = (String)((JComboBox)e.getSource()).getSelectedItem();
			
		} else if (actionCommand.equals(COMPUTE)) {
			
			if (accountFile == null) {
				showNotification("File conti non selezionato");
				return;
			}
			
			reportComposer.computeReport(reportFilename, accountFile);
		}
	}

}
